package org.jeecg.modules.ywSemiFinishedProductInventory.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.constant.CommonConstant;
import org.jeecg.constant.InventoryConstant;
import org.jeecg.modules.ywSemiFinishedProductInventory.entity.YwSemiFinishedProductInventoryInfo;
import org.jeecg.modules.ywSemiFinishedProductInventory.mapper.YwSemiFinishedProductInventoryInfoMapper;
import org.jeecg.modules.ywSemiFinishedProductInventory.service.IYwSemiFinishedProductInventoryInfoService;
import org.jeecg.modules.ywSemiFinishedProductInventory.vo.MobileInventoryRequest;
import org.jeecg.modules.ywSemiFinishedProductOut.entity.YwSemiFinishedProductOutDetail;
import org.jeecg.modules.ywSemiFinishedProductOut.entity.YwSemiFinishedProductOutMain;
import org.jeecg.modules.ywSemiFinishedProductOut.service.IYwSemiFinishedProductOutDetailService;
import org.jeecg.modules.ywSemiFinishedProductOut.service.IYwSemiFinishedProductOutMainService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 半成品库存
 * @Author: jeecg-boot
 * @Date: 2024-08-19
 * @Version: V1.0
 */
@Service
public class YwSemiFinishedProductInventoryInfoServiceImpl extends ServiceImpl<YwSemiFinishedProductInventoryInfoMapper, YwSemiFinishedProductInventoryInfo> implements IYwSemiFinishedProductInventoryInfoService {

    @Resource
    private YwSemiFinishedProductInventoryInfoMapper ywSemiFinishedProductInventoryInfoMapper;
    @Resource
    private IYwSemiFinishedProductOutMainService ywSemiFinishedProductOutMainService;
    @Resource
    private IYwSemiFinishedProductOutDetailService ywSemiFinishedProductOutDetailService;

    @Override
    public List<YwSemiFinishedProductInventoryInfo> selectList(String deptName, String productCode, String productName, String boxType) {
        return this.ywSemiFinishedProductInventoryInfoMapper.getList(deptName, productCode, productName, boxType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void mobile(MobileInventoryRequest mobileInventoryRequest) {
        // 想要移库的那条库存
        YwSemiFinishedProductInventoryInfo inventoryInfo = this.ywSemiFinishedProductInventoryInfoMapper.selectById(mobileInventoryRequest.getInventoryId());
        Assert.notNull(inventoryInfo, "不存在的库存信息,请联系管理员！");
        Assert.isTrue(inventoryInfo.getStatus().equals(CommonConstant.StatusEnum.YES.getCode()), "库存信息已被删除,请刷新后重试！");

        Assert.isTrue(mobileInventoryRequest.getBoxNum() <= inventoryInfo.getBoxNum(), "移库的箱子数量不能大于库存的箱子数量！");
        Assert.isTrue(mobileInventoryRequest.getInventoryNum() <= inventoryInfo.getInventoryNum(), "移库的箱子数量不能大于库存的箱子数量！");

        // 新增移库的库存
        YwSemiFinishedProductInventoryInfo inventory = new YwSemiFinishedProductInventoryInfo();
        inventory.setHistoryId(inventoryInfo.getId());
        inventory.setEnterId(inventoryInfo.getEnterId());
        inventory.setEnterDate(inventoryInfo.getEnterDate());
        inventory.setDeptName(inventoryInfo.getDeptName());
        inventory.setProductCode(inventoryInfo.getProductCode());
        inventory.setProductName(inventoryInfo.getProductName());
        inventory.setBoxType(inventoryInfo.getBoxType());
        inventory.setBoxVolume(inventoryInfo.getBoxVolume());
        inventory.setManufactureDate(inventoryInfo.getManufactureDate());
        inventory.setStatus(CommonConstant.StatusEnum.YES.getCode());
        inventory.setBoxNum(mobileInventoryRequest.getBoxNum());
        inventory.setInventoryNum(mobileInventoryRequest.getInventoryNum());
        inventory.setGoodsPosition(mobileInventoryRequest.getGoodsPosition());
        inventory.setInventoryVolume(inventory.getBoxVolume().multiply(BigDecimal.valueOf(mobileInventoryRequest.getBoxNum())));
        this.ywSemiFinishedProductInventoryInfoMapper.insert(inventory);

        // 更新现有库存
        inventoryInfo.setBoxNum(inventoryInfo.getBoxNum() - mobileInventoryRequest.getBoxNum());
        inventoryInfo.setInventoryNum(inventoryInfo.getInventoryNum() - mobileInventoryRequest.getInventoryNum());
        inventoryInfo.setInventoryVolume(inventoryInfo.getInventoryVolume().subtract(inventory.getInventoryVolume()));
        this.ywSemiFinishedProductInventoryInfoMapper.updateById(inventoryInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void outBatch(List<String> wantOutIdList, Date outDate, String billNo) {
        List<YwSemiFinishedProductInventoryInfo> inventoryInfoList = this.ywSemiFinishedProductInventoryInfoMapper.selectBatchIds(wantOutIdList);
        Assert.isTrue(inventoryInfoList.size() == wantOutIdList.size(), "出库失败,请检查数据是否异常！");

        YwSemiFinishedProductOutMain outMain = new YwSemiFinishedProductOutMain();
        outMain.setOutDate(outDate);
        outMain.setOutStatus(InventoryConstant.OutStatusEnum.PREPARE_OUT.getCode());
        ywSemiFinishedProductOutMainService.save(outMain);

        List<YwSemiFinishedProductOutDetail> outDetailList = inventoryInfoList.stream().map(data -> {
            YwSemiFinishedProductOutDetail outDetail = new YwSemiFinishedProductOutDetail();
            outDetail.setOutId(outMain.getId());
            outDetail.setBillNo(billNo);
            outDetail.setDeptName(data.getDeptName());
            outDetail.setProductCode(data.getProductCode());
            outDetail.setProductName(data.getProductName());
            outDetail.setBoxType(data.getBoxType());
            outDetail.setBoxVolume(data.getBoxVolume());
            outDetail.setBoxNum(data.getBoxNum());
            outDetail.setTotalNum(data.getInventoryNum());
            outDetail.setInventoryId(data.getId());
            outDetail.setEnterDate(data.getEnterDate());
            outDetail.setGoodsPosition(data.getGoodsPosition());
            outDetail.setInventoryNum(data.getInventoryNum());
            outDetail.setOutVolume(data.getInventoryVolume());
            outDetail.setStatus(CommonConstant.StatusEnum.YES.getCode());

            data.setBoxNum(0);
            data.setInventoryNum(0);
            return outDetail;
        }).collect(Collectors.toList());
        ywSemiFinishedProductOutDetailService.saveBatch(outDetailList);

        this.updateBatchById(inventoryInfoList);
    }

}
